package org.ResearchProject.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.ResearchProject.dto.Result;
import org.ResearchProject.pojo.Student;
import org.ResearchProject.pojo.Tutor;
import org.ResearchProject.service.StudentService;
import org.ResearchProject.service.TutorService;
import org.ResearchProject.service.WechatAutoService;
import org.ResearchProject.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

/**
 * 2024-12-15-13:33
 */
@Controller
@RequestMapping("/wx")
public class WxLoginController {
    @Autowired
    WechatAutoService wechatAutoService;
    @Autowired
    StudentService studentService;
    @Autowired
    TutorService tutorService;

    /**
     * 微信授权登录
     * @return
     */
    @RequestMapping("/login")
    public Result login(@RequestBody Map<String, String> payload){
        String code = payload.get("code");
        if (code == null) {
            return Result.error("code参数为空");
        }
        String response = wechatAutoService.login(code);
        JSONObject jsonObject = JSON.parseObject(response);
        String openid = jsonObject.getString("openid");
        String session_key = jsonObject.getString("session_key");
        // 查找是否存在openid
        Student student = studentService.selectByOpenid(openid);
        Tutor tutor = tutorService.selectByOpenid(openid);
        if (student!=null){
            //学生登录,返回学生信息,还有token
            Map<String, Object> claims = new HashMap<>();
            claims.put("username", student.getUsername());
            claims.put("auth", student.getAuth());
            claims.put("id", student.getId());
            String token = JwtUtil.genToken(claims);
            Map<String, Object> msg = new HashMap<>();
            msg.put("student", student);
            msg.put("token", token);
            return Result.success(msg);
        }
        if (tutor!=null){
            //导师登录
            Map<String, Object> claims = new HashMap<>();
            claims.put("username", tutor.getUsername());
            claims.put("auth", tutor.getAuth());
            claims.put("id", tutor.getId());
            String token = JwtUtil.genToken(claims);
            Map<String, Object> msg = new HashMap<>();
            msg.put("tutor", tutor);
            msg.put("token", token);
            return Result.success(msg);
        }
        //第一次登录,那么随机生成其他数据.之后让用户自己去修改
        String username = "wx_"+openid;
        String password = "123456";
        String email = "wx_"+openid+"@qq.com";
        String phoneNum = "18888888888";
        String address = "wx_address";
        String avatar = "https://s1.ax1x.com/2023/04/07/pSK5Oj.jpg";
        int gender = 1;
        String name = "wx_name";
        int auth = 1;
        Student student1 = new Student(username, password, email, phoneNum, address, avatar, gender, name, auth, openid);
        studentService.addStudent(student1);
        // 待改
        return Result.success(response);
    }
}
